﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;


namespace SqlAzureService
{
    public class SqlData : AzureConnect.WcfConfig
    {

        public override bool ExecuteRequest()
        {
            try
            {
                //create the proxy instance with bindings and endpoint the base class
                //configuration control has created for this
                SalesSvc.SalesClient cust =
                    new SalesSvc.SalesClient(this.FedBinding,
                     this.WcfEndpointAddress);

                //configure the channel so we can call it with 
                //FederatedClientCredentials. 
                SPChannelFactoryOperations.ConfigureCredentials<SalesSvc.ISales>
                     (cust.ChannelFactory,
                     Microsoft.SharePoint.SPServiceAuthenticationMode.Claims);

                //create a channel to the WCF endpoint using the 
                //token and claims of the current user
                SalesSvc.ISales claimsWCF =
                    SPChannelFactoryOperations.CreateChannelActingAsLoggedOnUser
                    <SalesSvc.ISales>(cust.ChannelFactory,
                     this.WcfEndpointAddress,
                    new Uri(this.WcfEndpointAddress.Uri.AbsoluteUri));

                //set the client property for the base class
                this.WcfClientProxy = claimsWCF;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            //now that the configuration is complete, call the method
            return base.ExecuteRequest();
        }

    }
}
